package com.apes.fn.server.workDesk.model;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.frame.model.Dept;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 车辆总控信息
 *
 * @author lws
 * @create 2022-11-03 17:20
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VehicleControl.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "dept_id, fn_customer_expand_id")
})
public class VehicleControl extends Model {
    public static final String CONST_TABLE_NAME = "vehicle_control";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "车辆总控信息")
    @Comment("编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("门店")
    private Dept dept;

    @States(name = "车辆状态", values = {
            @State(id = "ZK000751", name = "开始"),
            @State(id = "ZK000752", name = "待开单"),
            @State(id = "ZK000753", name = "待交车"),
            @State(id = "ZK000754", name = "在店"),
            @State(id = "ZK000755", name = "内协中"),
            @State(id = "ZK000756", name = "未结算交车"),
            @State(id = "ZK000757", name = "结单交车"),
            @State(id = "ZK000758", name = "结束")
    })
    @Column(name = "state", length = 32)
    private String state = "ZK000751";

    @Column(name = "finish_date")
    @Comment("完成日期")
    private Date finishDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_expand_id", referencedColumnName = "id", nullable = false)
    @Comment("赋能客户车辆扩展信息")
    private FnCustomerExpand fnCustomerExpand;

//    @Column(name = "is_Update_Expand", length = 120)
//    @Comment("扩展表编码是否可修改")
//    private String isUpdateExpand;//innerCreate 内协创建(可修改) //innerUpdate 内协创建(已修改)

    @OneToMany(mappedBy = "vehicleControl", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<VehicleControlItem> vehicleControlItems = new ArrayList<VehicleControlItem>(); //从表
    public List<VehicleControlItem> getVehicleControlItems(){
        for(VehicleControlItem vehicleControlItems : vehicleControlItems){
            vehicleControlItems.setVehicleControl(this);
        }
        return vehicleControlItems;
    }
}
